<?php
   
    include_once 'Classes/User.class.php';
    include_once 'DAO/UserDAO.class.php';

    // Elegxos dedomenwn
    $error = '0';
    // Username: epitrepoume mono grammata kai arithmous
    $username = trim($_POST['username']);
    $regexp = '/^[a-z]*[a-z0-9]*$/i';
    if(!preg_match($regexp, $username)){
        $error = '1';
    }
    // Password: Prepei na periexei toulaxiston 5 xarakthres
    $password = trim($_POST['password']);
    if(strlen($password) < 5){
        $error = $error . '2';
    }
    // Email: elegxoume ean einai keno kai thn morfh
    $regexp = '/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i';
    $email = trim($_POST['email']);
    if(!preg_match($regexp, $email)){
        $error = $error . '3';
    }

    // An den yparxei lathos dhmiourgoume neo xrhsth sth bash
    if($error == '0'){
        $newuser = new User();
        $newuser->username = $username;
        $newuser->passwordhash = sha1($password);
        $newuser->email = $email;
        // Eisagwgh sthn bash
        $userDAO = new UserDAO();
        if($userDAO->insert($newuser)){
            header('Location: registration.php?success=1');
        }
        else{
            $error = '-1';
            header("Location: registration.php?error=$error");
        }
   }
   else{
       header("Location: registration.php?error=$error");
   }
    

?>
